/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bill.bikes;

import java.util.Date; 
import java.util.List;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Aganguly
 */
@WebService
@Stateless()
public class Biller {
    
    @PersistenceContext(unitName = "BillingCentralPU")
    EntityManager em;

    /**
     * Web service operation
     */
    @WebMethod(operationName = "createBillEntry")
    public Long createBillEntry(@WebParam(name = "orderId") Long orderId, @WebParam(name = "billDate") Date billDate) {
        Billrecords record = new Billrecords();
        record.setBillDate(billDate);
        record.setOrderId(orderId);
        record.setStatus(BillStatus.Pending.toString());
        em.persist(record);
        return record.getBillId();
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "getBillingStatus")
    public String getBillingStatus(@WebParam(name = "billingId") long billingId) {
        Billrecords record = em.find(Billrecords.class, billingId);
        if (record != null) {
            return record.getStatus();
        }
        return null;
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "getBillsbyOrder")
    public List<String> getBillsbyOrder(@WebParam(name = "orderId") long orderId) {
        List<String> r = null;
        javax.persistence.Query q = em.createQuery("SELECT b FROM Billrecords b WHERE b.billDate = :billDate");
        q.setParameter("billDate", orderId);
        List<Billrecords> resultList = (List<Billrecords>) q.getResultList();
        if (resultList != null) {
            for (Billrecords b : resultList) {
                r.add("Bill Id: " + b.getBillId() + " OrderId:" + b.getOrderId() + " Status: " + b.getStatus());
            }
        }
        return r;
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "getBillsbyStatus")
    public List<String> getBillsbyStatus(@WebParam(name = "status") String status) {
        List<String> r = null;
        javax.persistence.Query q = em.createQuery("SELECT b FROM Billrecords b WHERE b.status = :status");
        q.setParameter("status", status);
        List<Billrecords> resultList = (List<Billrecords>) q.getResultList();
        if (resultList != null) {
            for (Billrecords b : resultList) {
                r.add("Bill Id: " + b.getBillId() + " OrderId:" + b.getOrderId() + " Status: " + b.getStatus());
            }
        }
        return r;
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "getAllBills")
    public List<String> getAllBills() {
        List<String> r = null;
        javax.persistence.Query q = em.createQuery("SELECT b FROM Billrecords b");
        List<Billrecords> resultList = (List<Billrecords>) q.getResultList();
        if (resultList != null) {
            for (Billrecords b : resultList) {
                r.add("Bill Id: " + b.getBillId() + " OrderId:" + b.getOrderId() + " Status: " + b.getStatus());
            }
        }
        return r;
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "setBillingStatusComplete")
    public Boolean setBillingStatusComplete(@WebParam(name = "billId") long billId) {
        Billrecords record = em.find(Billrecords.class, billId);
        if (record != null) {
            record.setStatus(BillStatus.Complete.toString());
            return true;
        }
        return null;
    }
}
